package com.hu.zhcc.shiro.controller;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.bo.*;
import com.hu.zhcc.shiro.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 体验上传
 */
@RestController
@RequestMapping("/tOrder")
public class TOrderController {

    @Autowired
    private TOrderService tOrderService;

    @Autowired
    private OrderSettingService orderSettingService;

    @Autowired
    private HMemberofthefileService hMemberofthefileService;

    @Autowired
    private CheckHabitusService checkHabitusService;

    @Autowired
    private CheckMindService checkMindService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("")
    public HMemberofthefile saveTOrder(@RequestBody TOrder tOrder) {
        HMemberofthefile select = hMemberofthefileService.select(tOrder.getHMemberofthefile().gethNumber());
        String pwd = "";
        String number = "";
        if(select==null){
            tOrder.getHMemberofthefile().sethFn("1");
            number = tOrder.getHMemberofthefile().gethNumber();
            pwd = tOrder.getHMemberofthefile().gethNumber().substring(tOrder.getHMemberofthefile().gethNumber().length() - 6);
            tOrder.getHMemberofthefile().sethPassword(pwd);
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            tOrder.getHMemberofthefile().sethRegistration(dateFormat.format( now ));
            tOrder.getHMemberofthefile().sethThehealthmanager("");
            tOrder.getHMemberofthefile().sethFilestatus("1");
            hMemberofthefileService.insertSelective(tOrder.getHMemberofthefile());
            HMemberofthefile select1 = hMemberofthefileService.select(tOrder.getHMemberofthefile().gethNumber());
            select1.sethFn("202012120"+select1.gethId());
            tOrder.setMemberId(select1.gethId());
            hMemberofthefileService.updateByPrimaryKeySelective(select1);
        }else{
            tOrder.setMemberId(select.gethId());
        }
        OrderSetting order = orderSettingService.getOrder(tOrder.getOrderdate());
        TOrder se = tOrderService.se(tOrder);
        if(se==null) {
            if (order != null && order.getNumber() != order.getReservations() && order.getReservations() < order.getNumber()) {
                order.setReservations(order.getReservations() + 1);
                orderSettingService.edit(order);
                int i = tOrderService.insertSelective(tOrder);
                if (select == null) {
                    HMemberofthefile hMemberofthefile = new HMemberofthefile();
                    hMemberofthefile.sethNumber(number);
                    hMemberofthefile.sethPassword(pwd);
                    System.out.println(hMemberofthefile);
                    HMemberofthefile login = hMemberofthefileService.login(hMemberofthefile);
                    System.out.println(login);
                    return login;
                }
                HMemberofthefile hMemberofthefile1 = new HMemberofthefile();
                hMemberofthefile1.sethId(1000);
                return hMemberofthefile1;
            }
        }else{
            HMemberofthefile hMemberofthefile2 = new HMemberofthefile();
            hMemberofthefile2.sethId(1001);
            return hMemberofthefile2;
        }
        return null;
    }

    @GetMapping("/tOrderAll")
    private Page<TOrder> findAll(int offset, int limit, String value) {
        return tOrderService.findToList(offset, limit, value);
    }

    @GetMapping("/tOrderList")
    private Page<TOrder> find(int offset, int limit, String value) {
        return tOrderService.findTo(offset, limit, value);
    }

    @GetMapping("/tOrderKey/{id}")
    private TOrder getMemberById(@PathVariable("id") int id) {
        return tOrderService.selectByPrimaryKey(id);
    }

    @DeleteMapping("/removeTOrder/{id}")
    private int removeMember(@PathVariable("id") int id) {
        TOrder tOrder = tOrderService.selectByPrimaryKey(id);
        OrderSetting order = orderSettingService.getOrder(tOrder.getOrderdate());
        System.out.println(order);
        order.setReservations(order.getReservations() - 1);
        System.out.println(order);
        orderSettingService.edit(order);
        return tOrderService.deleteByPrimaryKey(id);
    }

    //还未修改
    @PutMapping("/updateTOrder")
    private int updateMember(@RequestBody TOrder tOrder) {
        tOrder.setOrderstatus("已到诊");
        System.out.println(tOrder);
        return tOrderService.updateByPrimaryKeySelective(tOrder);
    }

    @PutMapping("/updateTOrderA")
    private int updateMemberA(@RequestBody TOrder tOrder) {
        tOrder.setOrderstatus("已到诊");

        HMemberofthefile hMemberofthefile = hMemberofthefileService.selectByPrimaryKey(tOrder.getMemberId());
        String gethFn = hMemberofthefile.gethFn();
        String gethName = hMemberofthefile.gethName();
        String orderdate = tOrder.getOrderdate();
        String gethSex = hMemberofthefile.gethSex();
        int gethAge = Integer.parseInt(hMemberofthefile.gethAge());
        String gethThehealthmanager = hMemberofthefile.gethThehealthmanager();//健康管理师

        CheckHabitus checkHabitus = new CheckHabitus();
        checkHabitus.setFilenumber(gethFn);
        checkHabitus.setName(gethName);
        checkHabitus.setValuationdate(orderdate);
        checkHabitusService.AddHabitus(checkHabitus);

        CheckMind checkMind = new CheckMind();
        checkMind.setFilenumber(gethFn);
        checkMind.setName(gethName);
        checkMind.setValuationdate(orderdate);
        checkMindService.AddMind(checkMind);

        Category category = new Category();
        category.setFilenumber(gethFn);
        category.setName(gethName);
        category.setSex(gethSex);
        category.setAge(gethAge);
        category.setOperator(gethThehealthmanager);
        categoryService.insert(category);
        return tOrderService.updateByPrimaryKeySelective(tOrder);
    }

    @GetMapping("/tOrderSelect")
    private Page<TOrder> select(int offset, int limit, String value) {
        return tOrderService.select(offset, limit, value);
    }

    /**
     * 会员单表
     */
    @GetMapping("/tOrderMemberNoun")
    private Page<HMemberofthefile> tOrderMemberNoun(int offset, int limit, String value, String startTime, String endTime) {
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        if (value.equals("a") || value.equals("") || value.equals("b")){
            return hMemberofthefileService.findhmListNuon(offset, limit ,value, map);
        }
        return null;
    }

    /**
     * 会员多表
     */
    @GetMapping("/tOrderNoun")
    private Page<TOrder> tOrderNoun(int offset, int limit, String value, String startTime, String endTime) {
        Map map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return tOrderService.selectNoun(offset,limit,value,map);
    }


    @GetMapping("/tOrderKeyId/{id}")
    private TOrder getById(@PathVariable("id") int id) {
        System.out.println("123");
        System.out.println(id);
        return tOrderService.selectId(id);
    }

}
